package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.AddressPickerManager;
import com.ssyc.common.manager.PickChooseManager;
import com.ssyc.common.manager.PickerManager;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.dialog.CustomDialogManager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.adapter.PopLvAdapter;
import com.ssyc.gsk_teacher.adapter.TeamRvAdapter;
import com.ssyc.gsk_teacher.bean.CommitResultInfo;
import com.ssyc.gsk_teacher.bean.StudentInfo;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import me.iwf.photopicker.PhotoPicker;
import me.shaohui.advancedluban.Luban;
import me.shaohui.advancedluban.OnCompressListener;
import okhttp3.Call;

/**
 * 添加新学生
 * Created by Administrator on 2018/6/27 0027.
 */

public class TeacherAddStudentActivity extends BaseActivity implements View.OnClickListener {

    //记录从班级小组界面传递过来的学生对象
    private StudentInfo info;

    private CircleImageView ivUserHeadimg;
    private TextView etUserChineseName;
    private TextView etUserEnglishName;
    private TextView tvUserSex;
    private TextView tvUserBirthday;
    private TextView etUserTel;
    private TextView tvUserAddress;
    private TextView etUserSchool;
    private TextView etUserGrade;
    private TextView etUserLove;
    private TextView tvUserState;

    //跳转到编辑学生信息的请求码
    public static final int CHINESERNAME = 0X110;
    public static final int ENGLISEHNAME = 0X120;
    public static final int PHONE = 0X130;
    public static final int SCHOOL = 0X140;
    public static final int GRADE = 0X150;
    public static final int LOVE = 0X160;
    public static final int STATE = 0X170;
    //弹框的通用适配器
    private PopLvAdapter popLvAdapter;

    //选择头像图片的地址
    private String headPath;
    //记录编辑的中文名
    private String chineseName, oldChineseName;
    //记录编辑的英文名
    private String englishName, oldEnglishName;
    //记录选择的性别
    private String sex, oldSex;
    //记录选择的生日
    private String time, oldTime;
    //记录填写的电话
    private String tel, oldTel;
    //记录选择的地址
    private String address, oldAddress;
    //记录填写的学校
    private String school, oldSchool;
    //记录填写的年级
    private String grade, oldGrade;
    //记录填写的兴趣爱好
    private String love, oldLove;
    //记录自定义的缴费状态
    private String state, oldState;
    private RelativeLayout rlUserIcon;
    private RelativeLayout rlUserChineseNmae;
    private RelativeLayout rlUserEnglishNmae;
    private RelativeLayout rlUserSex;
    private RelativeLayout rlUserBirthday;
    private RelativeLayout rlUseTel;
    private RelativeLayout rlUserAddress;
    private RelativeLayout rlUserSchool;
    private RelativeLayout rlUserGrade;
    private RelativeLayout rlUserLove;
    private RelativeLayout rlUserState;
    private ImageView ivBack;
    private Button btCommit;
    private TextView tvTitle;
    private ImageView iv6;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_add_student;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //获取Intent数据
        initIntent();
    }


    /**
     * 初始化视图
     */
    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        ivUserHeadimg = findViewById(R.id.iv_user_headimg);
        ivUserHeadimg.setOnClickListener(this);
        etUserChineseName = findViewById(R.id.et_user_chinese_name);
        etUserEnglishName = findViewById(R.id.et_user_english_name);
        tvUserSex = findViewById(R.id.tv_user_sex);
        tvUserBirthday = findViewById(R.id.tv_user_birthday);
        etUserTel = findViewById(R.id.et_user_tel);
        tvUserAddress = findViewById(R.id.tv_user_address);
        etUserSchool = findViewById(R.id.et_user_school);
        etUserGrade = findViewById(R.id.et_user_grade);
        etUserLove = findViewById(R.id.et_user_love);
        tvUserState = findViewById(R.id.tv_user_state);
        iv6 = findViewById(R.id.iv6);

        rlUserIcon = findViewById(R.id.rl_user_icon);
        rlUserIcon.setOnClickListener(this);

        rlUserChineseNmae = findViewById(R.id.rl_user_chinese_name);
        rlUserChineseNmae.setOnClickListener(this);


        rlUserEnglishNmae = findViewById(R.id.rl_user_english_name);
        rlUserEnglishNmae.setOnClickListener(this);

        rlUserSex = findViewById(R.id.rl_user_sex);
        rlUserSex.setOnClickListener(this);

        rlUserBirthday = findViewById(R.id.rl_user_birthday);
        rlUserBirthday.setOnClickListener(this);


        rlUseTel = findViewById(R.id.rl_user_tel);
        rlUseTel.setOnClickListener(this);

        rlUserAddress = findViewById(R.id.rl_user_address);
        rlUserAddress.setOnClickListener(this);

        rlUserSchool = findViewById(R.id.rl_user_school);
        rlUserSchool.setOnClickListener(this);

        rlUserGrade = findViewById(R.id.rl_user_grade);
        rlUserGrade.setOnClickListener(this);

        rlUserLove = findViewById(R.id.rl_user_love);
        rlUserLove.setOnClickListener(this);

        rlUserState = findViewById(R.id.rl_user_state);
        rlUserState.setOnClickListener(this);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);

        btCommit = findViewById(R.id.bt_commit);
        btCommit.setOnClickListener(this);

    }

    /**
     * 获取Intent对象
     */
    private void initIntent() {
        //证明是从班级信息界面点击小组上有学生的头像跳转过来的
        info = (StudentInfo) getIntent().getSerializableExtra("studentinfo");
        if (null != info) {
            //设置初始状态
            //修改标题
            tvTitle.setText(info.getName());
            //头像
            String headUrl = HttpAdress.HEADIMGURL + info.getIcon() + ".jpg";
            ImageUtil.displayImage(this, ivUserHeadimg, headUrl);

            //中文名
            oldChineseName = info.getName();
            chineseName = info.getName();
            etUserChineseName.setText(oldChineseName);

            //英文名
            oldEnglishName = info.getNickname();
            englishName = info.getNickname();
            etUserEnglishName.setText(oldEnglishName);

            //性别
            sex = info.getGender();
            oldSex = info.getGender();
            tvUserSex.setText(oldSex);

            //生日
            time = info.getBirthday();
            oldTime = info.getBirthday();
            tvUserBirthday.setText(oldTime);

            //电话
            tel = info.getTel();
            oldTel = info.getTel();
            etUserTel.setText(oldTel);
            iv6.setVisibility(iv6.INVISIBLE);


            //地址
            address = info.getAddress();
            oldAddress = info.getAddress();
            tvUserAddress.setText(oldAddress);


            //学校
            school = info.getSchool();
            oldSchool = info.getSchool();
            etUserSchool.setText(oldSchool);

            //年级
            grade = info.getGrade();
            oldGrade = info.getGrade();
            etUserGrade.setText(oldGrade);

            //兴趣爱好
            love = info.getFavorite();
            oldLove = info.getFavorite();
            etUserLove.setText(oldLove);

            //缴费状态
            state = info.getPayment();
            oldState = info.getPayment();
            tvUserState.setText(oldState);
        }
    }


    /**
     * 判断是否修改了学生信息的方法
     *
     * @return
     */
    private boolean hasUpdate() {
        boolean res = false;
        //修改头像
        if (!TextUtils.isEmpty(headPath) || (!TextUtils.isEmpty(oldChineseName) && !TextUtils.isEmpty(chineseName) && !oldChineseName.equals(chineseName)) ||
                (!TextUtils.isEmpty(englishName) && !TextUtils.isEmpty(oldEnglishName) && !englishName.equals(oldEnglishName)) ||
                (!TextUtils.isEmpty(oldSex) && !TextUtils.isEmpty(sex) && !oldSex.equals(sex)) ||
                (!TextUtils.isEmpty(time) && !TextUtils.isEmpty(oldTime) && !time.equals(oldTime)) ||
                (!TextUtils.isEmpty(address) && !TextUtils.isEmpty(oldAddress) && !address.equals(oldAddress)) ||
                (!TextUtils.isEmpty(school) && !TextUtils.isEmpty(oldSchool) && !school.equals(oldSchool)) ||
                (!TextUtils.isEmpty(grade) && !TextUtils.isEmpty(oldGrade) && !grade.equals(oldGrade)) ||
                (!TextUtils.isEmpty(state) && !TextUtils.isEmpty(oldState) && !state.equals(oldState))) {
            res = true;
        }
        return res;
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_commit) {
            //点击标题栏的创建,进行判空后进行服务器上传信息的操作
            if (info == null) {
                //创建新学生
                judgeAllByCreateNewStudent();
            } else {
                //修改学生信息
                judgeAllByUpdateStudent();
            }
        } else if (id == R.id.iv_user_headimg || id == R.id.rl_user_icon) {
            //点击用户头像跳转到相册选择界面或者进行系统拍照功能
            chooseHeadImgAction();
        } else if (id == R.id.rl_user_chinese_name) {
            //点击姓名(中文)的相对布局
            editStudentAction("姓名", CHINESERNAME);
        } else if (id == R.id.rl_user_english_name) {
            //点击姓名(英文)的相对布局
            editStudentAction("英文名", ENGLISEHNAME);
        } else if (id == R.id.rl_user_sex) {
            //点击性别的相对布局
            choosePop(0);
        } else if (id == R.id.rl_user_birthday) {
            //点击生日的相对布局
            chooseBirthdayPop();
        } else if (id == R.id.rl_user_tel) {
            //点击电话的相对布局
            if (info != null) {
                return;
            } else {
                editStudentAction("电话", PHONE);
            }
        } else if (id == R.id.rl_user_address) {
            //点击地址的相对布局
            chooseAddressPop();
        } else if (id == R.id.rl_user_school) {
            //点击学校的相对布局
            editStudentAction("学校", SCHOOL);
        } else if (id == R.id.rl_user_grade) {
            //点击年级的相对布局
            choosePop(2);
        } else if (id == R.id.rl_user_state) {
            //点击缴费状态的相对布局
            choosePop(1);
        } else if (id == R.id.iv_back) {
            finish();
        }
    }


    /**
     * 弹出选择地址的弹框
     */
    private void chooseAddressPop() {
        AddressPickerManager.showAddressPicker(this, new AddressPickerManager.onAddressSelectedListener() {
            @Override
            public void onChooseAddress(String res) {
                address = res;
                tvUserAddress.setText(address);
            }
        });
    }

    /**
     * 弹出选择生日的时间选择器,精确到年月日
     */
    private void chooseBirthdayPop() {

        PickerManager.showPickerByYeaerAndMonthAndDayBithday(this, new PickerManager.onSelectedListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                time = DateUtils.getTime(date, "yyyy-MM-dd");
                tvUserBirthday.setText(time);
            }
        });

    }


    /**
     * 通用弹框的PopUpWindow
     */
    private void choosePop(final int type) {
        final List<String> contents = new ArrayList<>();
        if (type == 0) {
            contents.add("男");
            contents.add("女");
        } else if (type == 1) {
            contents.add("已缴");
            contents.add("未缴");
            contents.add("自定义");
        } else if (type == 2) {
            for (int i = 1; i < 10; i++) {
                contents.add(i + "");
            }
        }
        //弹框
        PopUpManager.showPop(this, R.layout.teacher_pop_item_common, 0.3f, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //标题
                TextView tvTitle = view.findViewById(R.id.tv_title);
                //列表
                ListView lv = view.findViewById(R.id.lv);
                //取消
                TextView tvCancel = view.findViewById(R.id.tv_cancel);

                if (type == 0) {
                    tvTitle.setText("性别");
                } else if (type == 1) {
                    tvTitle.setText("缴费状态");
                } else if (type == 2) {
                    tvTitle.setText("年级");
                }

                //设置列表数据
                popLvAdapter = new PopLvAdapter(TeacherAddStudentActivity.this, contents,
                        R.layout.teacher_lv_item_pop_common);
                lv.setAdapter(popLvAdapter);

                //条目点击事件
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                        if (type == 0) {
                            //性别
                            sex = contents.get(pos);
                            tvUserSex.setText(contents.get(pos));
                            PopUpManager.dismiss();
                        } else if (type == 1) {
                            //缴费状态
                            if (pos != contents.size() - 1) {
                                //不是最后一个自定义
                                state = contents.get(pos);
                                tvUserState.setText(contents.get(pos));
                            } else {
                                //跳转到自定义
                                editStudentAction("缴费状态", STATE);
                            }
                            PopUpManager.dismiss();
                        } else if (type == 2) {
                            grade = contents.get(pos);
                            etUserGrade.setText(contents.get(pos));
                            PopUpManager.dismiss();
                        }
                    }
                });

                //取消的点击事件
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });
            }
        });
    }

    /**
     * 跳转到编辑学生信息的界面
     */
    private void editStudentAction(String title, int type) {
        Intent intent = new Intent(this, TeacherEditStudentActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        startActivityForResult(intent, type);
    }

    /**
     * 选择头像的方法
     */
    private void chooseHeadImgAction() {
        PickChooseManager.startPhotoPickActivity(this, 1);
    }


    /**
     * 判断创建学生资料的完整性然后上传服务器的方法
     */
    private void judgeAllByCreateNewStudent() {
        //判断是否选择了头像
        if (TextUtils.isEmpty(headPath)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择头像", 0).show();
            return;
        }

        //判断是否输入了中文姓名
        if (TextUtils.isEmpty(chineseName)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写中文名称", 0).show();
            return;
        }

        //判断是否输入了英文姓名
        if (TextUtils.isEmpty(englishName)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写英文名称", 0).show();
            return;
        }

        //判断是否选择了性别
        if (TextUtils.isEmpty(sex)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择性别", 0).show();
            return;
        }

        //判断是否选择了生日
        if (TextUtils.isEmpty(time)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择生日", 0).show();
            return;
        }

        //判断是否填写了电话
        if (TextUtils.isEmpty(tel)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写电话", 0).show();
            return;
        }

        //判断是否选择了地址
        if (TextUtils.isEmpty(address)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择地址", 0).show();
            return;
        }

        //判断是否填写了学校
        if (TextUtils.isEmpty(school)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写学校", 0).show();
            return;
        }

        //判断是否填写了年级
        if (TextUtils.isEmpty(grade)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写年级", 0).show();
            return;
        }

        //判断是否填写了缴费状态
        if (TextUtils.isEmpty(state)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择或填写缴费状态", 0).show();
            return;
        }

        CustomDialogManager.show(this, "创建中...");

        //文件上传和其它上传
        File headFile = new File(headPath);

        //使用Luban压缩后上传文件
        Luban.compress(this, headFile).setMaxSize(150).setMaxHeight(1280).setMaxWidth(720)
                .putGear(Luban.CUSTOM_GEAR).launch(new OnCompressListener() {
            @Override
            public void onStart() {
                //开始压缩
            }

            @Override
            public void onSuccess(File file) {
                try {
                    //压缩成功,上传文件
                    Map<String, String> map = new HashMap<>();
                    map.put("type", "3");
                    map.put("c", AccountUtils.getAccount(TeacherAddStudentActivity.this));
                    map.put("icon", tel + "_" + "0");
                    map.put("tel", tel);
                    map.put("grade", grade);
                    //取年月日
                    String[] days = time.split("-");
                    map.put("name", URLEncoder.encode(chineseName, "utf-8"));
                    map.put("nickname", URLEncoder.encode(englishName, "utf-8"));
                    map.put("year", URLEncoder.encode(days[0], "utf-8"));
                    map.put("month", URLEncoder.encode(days[1], "utf-8"));
                    map.put("day", URLEncoder.encode(days[2], "utf-8"));
                    map.put("address", URLEncoder.encode(address, "utf-8"));
                    map.put("payment", URLEncoder.encode(state, "utf-8"));
                    map.put("school", URLEncoder.encode(school, "utf-8"));
                    Log.i("test", URLEncoder.encode(sex, "utf-8") + "---");
                    map.put("gender", URLEncoder.encode(sex, "utf-8"));
                    map.put("user_type", "1");
                    map.put("platform", "8");
                    Map<String, File> fileMap = new HashMap<>();
                    fileMap.put(System.currentTimeMillis() + ".jpg", file);

                    OkHttpUtils.post().url(HttpAdress.CREATESTUDENT).params(map).files("picture", fileMap).params(map).build().execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            UiUtils.Toast("服务器异常,创建失败", false);
                            CustomDialogManager.dissmiss();
                        }

                        @Override
                        public void onResponse(String response, int id) {

                            CustomDialogManager.dissmiss();

                            if (!TextUtils.isEmpty(response)) {
                                CommitResultInfo info = null;
                                try {
                                    info = GsonUtil.jsonToBean(response, CommitResultInfo.class);
                                } catch (Exception e) {
                                    Log.i("test", "数据解析异常");
                                }
                                if (null != info) {
                                    if ("1".equals(info.getStatus())) {
                                        //返回成功
                                        UiUtils.Toast("创建学生成功", false);
                                        //通知页面刷新
                                        finish();
                                    } else if ("-1".equals(info.getStatus())) {
                                        UiUtils.Toast("创建失败,服务器处理异常", false);
                                    } else if ("2".equals(info.getStatus())) {
                                        UiUtils.Toast("创建失败,中文名重复", false);
                                    } else if ("3".equals(info.getStatus())) {
                                        UiUtils.Toast("创建失败,英文名重复", false);
                                    } else if ("0".equals(info.getStatus())) {
                                        UiUtils.Toast("创建失败,账号已经注册过了", false);
                                    }
                                }
                            } else {
                                Log.i("test", "服务返回数据异常");
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                //压缩错误
            }
        });
    }




    /**
     * 判断是否修改了学生信息然后上传服务器
     */
    private void judgeAllByUpdateStudent() {

        //修改过,重新上传学生信息
        //判断是否输入了中文姓名
        if (TextUtils.isEmpty(chineseName)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写中文名称", 0).show();
            return;
        }

        //判断是否输入了英文姓名
        if (TextUtils.isEmpty(englishName)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写英文名称", 0).show();
            return;
        }

        //判断是否选择了性别
        if (TextUtils.isEmpty(sex)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择性别", 0).show();
            return;
        }

        //判断是否选择了生日
        if (TextUtils.isEmpty(time)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择生日", 0).show();
            return;
        }

        //判断是否选择了地址
        if (TextUtils.isEmpty(address)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择地址", 0).show();
            return;
        }

        //判断是否填写了学校
        if (TextUtils.isEmpty(school)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写学校", 0).show();
            return;
        }

        //判断是否填写了年级
        if (TextUtils.isEmpty(grade)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请填写年级", 0).show();
            return;
        }

        //判断是否填写了缴费状态
        if (TextUtils.isEmpty(state)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, "请选择或填写缴费状态", 0).show();
            return;
        }

        //判断是否修改学生的信息
        if (hasUpdate()) {

            CustomDialogManager.show(this, "修改中...");

            if (!TextUtils.isEmpty(headPath)) {
                //证明修改了头像,需要上传头像
                //压缩头像后上传文件上传和其它上传
                File headFile = new File(headPath);

                //使用Luban压缩后上传文件
                Luban.compress(this, headFile).setMaxSize(150).setMaxHeight(1280).setMaxWidth(720)
                        .putGear(Luban.CUSTOM_GEAR).launch(new OnCompressListener() {
                    @Override
                    public void onStart() {
                        //开始压缩
                    }

                    @Override
                    public void onSuccess(File file) {
                        try {
                            //压缩成功,上传文件
                            Map<String, String> map = new HashMap<>();
                            map.put("type", "11");
                            map.put("c", AccountUtils.getAccount(TeacherAddStudentActivity.this));
                            String icon = info.getIcon();
                            if (!TextUtils.isEmpty(icon)) {
                                String[] icons = icon.split("_");
                                String passNewIcon = icons[0] + "_" + (Integer.parseInt(icons[1]) + 1);
                                map.put("icon", passNewIcon);
                            }
                            map.put("id", info.getId() + "");
                            map.put("name", URLEncoder.encode(chineseName, "utf-8"));
                            map.put("nickname", URLEncoder.encode(englishName, "utf-8"));
                            map.put("grade", grade);

                            //取年月日
                            String[] days = time.split("-");
                            map.put("year", URLEncoder.encode(days[0], "utf-8"));
                            map.put("month", URLEncoder.encode(days[1], "utf-8"));
                            map.put("day", URLEncoder.encode(days[2], "utf-8"));
                            map.put("address", URLEncoder.encode(address, "utf-8"));
                            map.put("payment", URLEncoder.encode(state, "utf-8"));
                            map.put("school", URLEncoder.encode(school, "utf-8"));
                            Log.i("test", URLEncoder.encode(sex, "utf-8") + "---");
                            map.put("gender", URLEncoder.encode(sex, "utf-8"));
                            map.put("user_type", "1");
                            map.put("platform", "8");
                            Map<String, File> fileMap = new HashMap<>();
                            fileMap.put(System.currentTimeMillis() + ".jpg", file);

                            OkHttpUtils.post().url(HttpAdress.UPDATESTUDENT).params(map).files("picture", fileMap).params(map).build().execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    UiUtils.Toast("服务器异常,修改失败", false);
                                    CustomDialogManager.dissmiss();
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    CustomDialogManager.dissmiss();
                                    if (!TextUtils.isEmpty(response)) {
                                        CommitResultInfo info = null;
                                        try {
                                            info = GsonUtil.jsonToBean(response, CommitResultInfo.class);
                                        } catch (Exception e) {
                                            Log.i("test", "数据解析异常");
                                        }
                                        if (null != info) {
                                            if ("1".equals(info.getStatus())) {
                                                //返回成功
                                                UiUtils.Toast("修改成功", false);
                                                //通知TeacherStudentFileActivity页面刷新
                                                BusInfo busInfo = new BusInfo();
                                                busInfo.setType(TeamRvAdapter.UPDATAPAGEDATA);
                                                EventBus.getDefault().post(busInfo);
                                                //通知页面刷新
                                                finish();
                                            } else if ("-1".equals(info.getStatus())) {
                                                UiUtils.Toast("修改失败,服务器处理异常", false);
                                            } else if ("2".equals(info.getStatus())) {
                                                UiUtils.Toast("修改失败,中文名重复", false);
                                            } else if ("3".equals(info.getStatus())) {
                                                UiUtils.Toast("修改失败,英文名重复", false);
                                            } else if ("0".equals(info.getStatus())) {
                                                UiUtils.Toast("修改失败,该学生不存在", false);
                                            }
                                        }
                                    } else {
                                        Log.i("test", "服务返回数据异常");
                                    }
                                }
                            });

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        //压缩错误
                    }
                });
            } else {
                try {
                    //没修改头像,不需要做Utf-8的处理
                    Map<String, String> map = new HashMap<>();
                    map.put("type", "11");
                    map.put("c", AccountUtils.getAccount(TeacherAddStudentActivity.this));
                    map.put("id", info.getId() + "");
                    map.put("icon", info.getIcon() + "");
                    map.put("name", chineseName);
                    map.put("nickname", englishName);
                    map.put("grade", grade);
                    //取年月日
                    String[] days = time.split("-");
                    map.put("year", days[0]);
                    map.put("month", days[1]);
                    map.put("day", days[2]);
                    map.put("address", address);
                    map.put("payment", state);
                    map.put("school", school);
                    map.put("gender", sex);
                    map.put("user_type", "1");
                    map.put("platform", "8");
                    Log.i("test", "type=11&c=" + AccountUtils.getAccount(TeacherAddStudentActivity.this)
                            + "&id=" + info.getId() + "&icon=" + info.getIcon() + "&name=" + chineseName + "&nickname=" + englishName +
                            "&grade=" + grade + "&year=" + days[0] + "&month=" + days[1] + "&day=" + days[2] + "&address=" + address +
                            "&payment=" + state + "&school=" + school + "&gender=" + sex + "&user_type=1&platform=8");
                    OkHttpUtils.post().url(HttpAdress.UPDATESTUDENT).params(map).params(map).build().execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            UiUtils.Toast("服务器异常,修改失败", false);
                            CustomDialogManager.dissmiss();
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            CustomDialogManager.dissmiss();
                            if (!TextUtils.isEmpty(response)) {
                                CommitResultInfo info = null;
                                try {
                                    info = GsonUtil.jsonToBean(response, CommitResultInfo.class);
                                } catch (Exception e) {
                                    Log.i("test", "数据解析异常");
                                }
                                if (null != info) {
                                    if ("1".equals(info.getStatus())) {
                                        //返回成功
                                        UiUtils.Toast("修改成功", false);
                                        //通知TeacherStudentFileActivity页面刷新
                                        BusInfo busInfo = new BusInfo();
                                        busInfo.setType(TeamRvAdapter.UPDATAPAGEDATA);
                                        EventBus.getDefault().post(busInfo);
                                        finish();
                                    } else if ("-1".equals(info.getStatus())) {
                                        UiUtils.Toast("修改失败,服务器处理异常", false);
                                    } else if ("2".equals(info.getStatus())) {
                                        UiUtils.Toast("修改失败,中文名重复", false);
                                    } else if ("3".equals(info.getStatus())) {
                                        UiUtils.Toast("修改失败,英文名重复", false);
                                    } else if ("0".equals(info.getStatus())) {
                                        UiUtils.Toast("修改失败,该学生不存在", false);
                                    }
                                }
                            } else {
                                Log.i("test", "服务返回数据异常");
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } else {
            SnackbarUtil.ShortSnackbar(llBaseMain, "未修改任何信息,不需要上传", Snackbar.LENGTH_SHORT).show();
        }
    }


    /**
     * 选择头像后的回调处理
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //选择相册图片后的回调
        if (requestCode == PhotoPicker.REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                ArrayList<String> photos =
                        data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                headPath = photos.get(0);
                //获取选择的头像地址
                ImageUtil.displayImage(this, ivUserHeadimg, headPath);
            }
        }


        //获取编辑后的学生的相关值
        if (data != null) {
            String result = data.getStringExtra("data");
            //选择编辑学生相关信息后的回调
            if (!TextUtils.isEmpty(result)) {
                if (requestCode == CHINESERNAME && resultCode == TeacherEditStudentActivity.CHINESERNAME) {
                    //中文名
                    chineseName = result;
                    etUserChineseName.setText(result);
                } else if (requestCode == ENGLISEHNAME && resultCode == TeacherEditStudentActivity.ENGLISEHNAME) {
                    //英语名
                    englishName = result;
                    etUserEnglishName.setText(result);
                } else if (requestCode == PHONE && resultCode == TeacherEditStudentActivity.PHONE) {
                    //电话
                    tel = result;
                    etUserTel.setText(result);
                } else if (requestCode == SCHOOL && resultCode == TeacherEditStudentActivity.SCHOOL) {
                    //学校
                    school = result;
                    etUserSchool.setText(result);
                } else if (requestCode == LOVE && resultCode == TeacherEditStudentActivity.LOVE) {
                    //兴趣爱好
                    love = result;
                    etUserLove.setText(result);
                } else if (requestCode == STATE && resultCode == TeacherEditStudentActivity.STATE) {
                    //缴费状态
                    state = result;
                    tvUserState.setText(result);
                }
            }
        }

    }

}
